package com.cndym.utils;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class ImagesUtils {
	/**
	 * @param filePath
	 *            需要添加水印的图片的路径
	 * @param imageMark
	 *            水印图片路径
	 */

	public static boolean createMark(String filePath, String imageMark, String textMark) {
		ImageIcon imgIcon = new ImageIcon(filePath);
		Image theImg = imgIcon.getImage();
		ImageIcon waterIcon = new ImageIcon(imageMark);
		Image waterImg = waterIcon.getImage();
		int width = theImg.getWidth(null);
		int height = theImg.getHeight(null);
		int w = waterImg.getWidth(null);
		int h = waterImg.getHeight(null);

		BufferedImage bimage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
		Graphics2D g = bimage.createGraphics();
		g.setColor(Color.red);
		g.setBackground(Color.white);
		g.drawImage(theImg, 0, 0, null);
		g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 0.5f));
		g.drawImage(waterImg, (width - w - 5), (height - h - 5), null);
		g.drawString("添加文字", width - 20, height - 5); // 添加文字
		g.dispose();
		try {
			FileOutputStream out = new FileOutputStream(filePath);
			JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
			JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bimage);
			param.setQuality(80f, true);
			encoder.encode(bimage, param);
			out.close();
			return true;
		} catch (Exception e) {
			return false;
		}

	}

	/**
	 * @param filePath
	 *            需要缩微的图片的路径
	 * @param size
	 *            缩微图片大小
	 */

	public static boolean imagesSize(String filePath, float size) {

		File file = new File(filePath);
		String filename = file.getName();
		String ext = filename.substring(filename.lastIndexOf(".") + 1, filename.length());
		try {
			String getpath = file.getCanonicalPath();
			String newName = filename.replaceAll("." + ext, "_min." + ext);
			String savList = getpath.replaceAll(filename, "min//");

			File getList = new File(savList);
			if (!getList.exists()) {
				getList.mkdir();
			}
			String savfile = getpath.replaceAll(filename, "min//" + newName);
			Image src = ImageIO.read(file);
			int old_w = src.getWidth(null); // 得到源图宽
			int old_h = src.getHeight(null); // 得到源图长
			int new_w = 0;
			int new_h = 0;
			float tempdouble;
			if (old_w > old_h) {
				tempdouble = old_w / size;
			} else {
				tempdouble = old_h / size;
			}
			new_w = Math.round(old_w / tempdouble);
			new_h = Math.round(old_h / tempdouble);// 计算新图长宽
			BufferedImage tag = new BufferedImage(new_w, new_h, BufferedImage.TYPE_INT_RGB);
			tag.getGraphics().drawImage(src, 0, 0, new_w, new_h, null); // 绘制缩小后的图
			FileOutputStream newimage = new FileOutputStream(savfile); // 输出到文件流
			JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(newimage); // 近JPEG编码
			encoder.encode(tag);
			return true;
		} catch (Exception e1) {
			System.out.println(e1);
			return false;
		}
	}

	/**
	 * 文件剪切
	 * 
	 * @param image
	 * @param x
	 * @param y
	 * @param w
	 * @param h
	 * @param smailImage
	 */
	public static void saveSubImage(String image, int x, int y, int w, int h, String smailImage) {
		try {
			File file = new File(image);
			InputStream inputStream = new FileInputStream(file);
			BufferedImage srcImage = ImageIO.read(inputStream);
			Rectangle subImageBounds = new Rectangle(x, y, w, h);
			if (subImageBounds.x < 0 || subImageBounds.y < 0 || subImageBounds.width - subImageBounds.x > srcImage.getWidth() || subImageBounds.height - subImageBounds.y > srcImage.getHeight()) {
				System.out.println("Bad   subimage   bounds");
				return;
			}
			BufferedImage subImage = srcImage.getSubimage(subImageBounds.x, subImageBounds.y, subImageBounds.width, subImageBounds.height);
			File subImageFile = new File(smailImage);
			String fileName = subImageFile.getName();
			String formatName = fileName.substring(fileName.lastIndexOf('.') + 1);
			ImageIO.write(subImage, formatName, subImageFile);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 创建临时图片 根据目录图片大小和自定义颜色
	 * @param path
	 * @param r
	 * @param g
	 * @param b
	 * @return
	 */
	public static BufferedImage creatTempImage(String path, int r, int g, int b) {
		File file = new File(path);
		InputStream inputStream;
		BufferedImage image = null;
		try {
			inputStream = new FileInputStream(file);
			BufferedImage srcImage = ImageIO.read(inputStream);
			int width = srcImage.getWidth();
			int height = 20;
			image = creatTempImage(width,height,r,g,b);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return image;
	}

	/**
	 * 根据自定义大小创建临时图片
	 * @param width
	 * @param height
	 * @param r
	 * @param g
	 * @param b
	 * @return
	 */
	public static BufferedImage creatTempImage(int width, int height, int r, int g, int b) {
		BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
		Graphics graphics = image.getGraphics();
		graphics.setColor(new Color(r, g, b));
		graphics.fillRect(0, 0, width, height);
		return image;
	}

	/**
	 * 添加文字
	 * @param bufferedImage
	 * @param text
	 * @return
	 */
	public static BufferedImage creatTempImageText(BufferedImage bufferedImage, String text) {
		Graphics graphics = bufferedImage.getGraphics();
		graphics.setColor(Color.red);
		graphics.setFont(new Font("Times New Roman", Font.HANGING_BASELINE, 14));
		graphics.drawString(text, 2, 12); // 添加文字
		return bufferedImage;
	}

	/**
	 * 添加图片
	 * @param bufferedImage
	 * @param path
	 * @return
	 */
	public static BufferedImage creatTempImagePic(BufferedImage bufferedImage, String path) {
		File file = new File(path);
		InputStream inputStream;
		try {
			inputStream = new FileInputStream(file);
			BufferedImage srcImage = ImageIO.read(inputStream);
			Graphics2D graphics = bufferedImage.createGraphics();
			graphics.drawImage(srcImage, 0, 0, null);
			graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 0.5f));
			graphics.drawImage(srcImage, 2, 12, null);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return bufferedImage;
	}

	/**
	 * 用生成的图片添加水印
	 * @param filePath
	 * @param waterImg
	 * @return
	 */
	public static boolean createMark(String filePath, BufferedImage waterImg) {
		ImageIcon imgIcon = new ImageIcon(filePath);
		Image theImg = imgIcon.getImage();
		int width = theImg.getWidth(null);
		int height = theImg.getHeight(null);
		int h = waterImg.getHeight(null);
		BufferedImage bimage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
		Graphics2D graphics = bimage.createGraphics();
		graphics.setColor(Color.red);
		graphics.setBackground(Color.white);
		graphics.drawImage(theImg, 0, 0, null);
		graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 0.5f));
		graphics.drawImage(waterImg, 0, (height - h), null);
		graphics.dispose();
		try {
			FileOutputStream out = new FileOutputStream(filePath);
			JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
			JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bimage);
			param.setQuality(80f, true);
			encoder.encode(bimage, param);
			out.close();
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	
	public static BufferedImage randomImage(String[] text){
		BufferedImage bufferedImage = new BufferedImage(52, 24, BufferedImage.TYPE_INT_RGB);
		Graphics2D graphics = bufferedImage.createGraphics();
		graphics.setColor(Color.white);
		graphics.setBackground(Color.white);
		graphics.fillRect(0, 0, 52, 24);
		graphics.setColor(Color.red);
		graphics.setFont(new Font("times new roman", Font.ITALIC, 20));
		for (int i=0; i<text.length;i++) {
			graphics.drawString(text[i], 1+(10*i), 20); // 添加文字
		}
		return bufferedImage;
	}
	
}
